<!DOCTYPE html>
<html>

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>类型模板管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="/plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="/plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="/css/style.css">
	<script src="/plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="/plugins/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/plugins/select2/select2.css" />
    <link rel="stylesheet" href="/plugins/select2/select2-bootstrap.css" />
    <script src="/plugins/select2/select2.min.js" type="text/javascript"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->
                
                    <div class="box-header with-border">
                        <h3 class="box-title">商品类型模板管理</h3>
                    </div>

                    <div class="box-body">

                        <!-- 数据表格 -->
                        <div class="table-box">

                            <!--工具栏-->
                            <div class="pull-left">
                                <div class="form-group form-inline">
                                    <div class="btn-group">
                                        <button type="button" class="btn btn-default" title="新建" data-toggle="modal" data-target="#editModal" ><i class="fa fa-file-o"></i> 新建</button>
                                        <button type="button" onclick="deleteCheck()" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button>
                                       
                                        <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
                                    </div>
                                </div>
                            </div>
                            <div class="box-tools pull-right">
                                <div class="has-feedback">
							                  分类模板名称：<input type="text" id="searchName" name="name">
									<button onclick="searchName()" class="btn btn-default">查询</button>
                                </div>
                            </div>
                            <!--工具栏/-->

			                  <!--数据列表-->
			                  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			                      <thead>
			                          <tr>
			                              <th class="" style="padding-right:0px">
			                                  <input id="selall" type="checkbox" class="icheckbox_square-blue">
			                              </th> 
										  <th class="sorting_asc">模板ID</th>
									      <th class="sorting">分类模板名称</th>
									      <th class="sorting">关联品牌</th>
									      <th class="sorting">关联规格</th>									     
									      <th class="sorting">扩展属性</th>													     						
					                      <th class="text-center">操作</th>
			                          </tr>
			                      </thead>
			                      <tbody id="typeTemplate">
			                          <tr>
			                              <td><input  type="checkbox"></td>			                              
				                          <td>1</td>
									      <td>手机</td>
									      <td>联想,华为,中兴</td>
									      <td>网络制式</td>									 
									      <td></td>		                                                                  
		                                  <td class="text-center">                                           
		                                 	  <button type="button" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#editModal" >修改</button>                                           
		                                  </td>
			                          </tr>
			                      </tbody>
			                  </table>
			                  <!--数据列表/-->                        
							  
							 
                        </div>
                        <!-- 数据表格 /-->
                        
                        
                        
                        
                     </div>
                    <!-- /.box-body -->
                       
<!-- 编辑窗口 -->
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-dialog" >
	<div class="modal-content">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
			<h3 id="myModalLabel">商品类型模板编辑</h3>
		</div>
		<div class="modal-body">							
			
			<table class="table table-bordered table-striped"  width="800px">
		      	<tr>
		      		<td>商品类型</td>
		      		<td><input  class="form-control" name="name" id="name" placeholder="商品类型">  </td>
		      	</tr>			   
		      	<tr>
		      		<td>关联品牌</td>
		      		<td>
						<select id="brand" multiple style="width: 100%;">
							<option th:each="brand :${brandList}" th:value="${brand.id}" th:text="${brand.name}"></option>
						</select>
		      		</td>
		      	</tr>
		      	<tr>
		      		<td>关联规格</td>
		      		<td>
						<select id="spec" multiple style="width: 100%;">
							<option th:each="spec :${specList}" th:value="${spec.id}" th:text="${spec.specName}"></option>
						</select>
		      		</td>
		      	</tr>	
		      	
		      	<tr>
		      		<td>扩展属性</td>
		      		<td>
		      			<div class="btn-group">
                             <button type="button" onclick="addTr()" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>
                                                                    
                        </div>
						<table class="table table-bordered table-striped"  width="800px">
							<thead>
								<tr>
								    <td><input type="checkbox" class="icheckbox_square-blue"></td>
									<td>属性名称</td>									
									<td>操作</td>
								</tr>
							</thead>

							<tbody id="cpTbody">
								<tr id="cpTr">
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" onclick="delTr(this)" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
								<tr>
									<td><input type="checkbox" class="icheckbox_square-blue" ></td>
									<td><input class="form-control" placeholder="属性名称" ></td>
									<td><button type="button" onclick="delTr(this)" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								</tr>
							</tbody>
						</table>	
           
		      		</td>
		      	</tr>	
		      	      
			 </table>				
			
		</div>
		<div class="modal-footer">						
			<button onclick="addTypeTemplate()" class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>
			<button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
		</div>
	  </div>
	</div>
</div>
  <!-- 修改窗口 -->
  <div class="modal fade" id="updateModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	  <div class="modal-dialog" >
		  <div class="modal-content">
			  <div class="modal-header">
				  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
				  <h3 id="myModalLabel1">商品类型模板编辑</h3>
			  </div>
			  <div class="modal-body">

				  <table class="table table-bordered table-striped"  width="800px">
					  <tr>
						  <input type="hidden" name="id" id="id">
						  <td>商品类型</td>
						  <td><input  class="form-control" name="name" id="nameUpdate" placeholder="商品类型">  </td>
					  </tr>
					  <tr>
						  <td>关联品牌</td>
						  <td>
							  <select id="brandUpdate" multiple style="width: 100%;">
								  <option th:each="brand :${brandList}" th:value="${brand.id}" th:text="${brand.name}"></option>
							  </select>
						  </td>
					  </tr>
					  <tr>
						  <td>关联规格</td>
						  <td>
							  <select id="specUpdate" multiple style="width: 100%;">
								  <option th:each="spec :${specList}" th:value="${spec.id}" th:text="${spec.specName}"></option>
							  </select>
						  </td>
					  </tr>

					  <tr>
						  <td>扩展属性</td>
						  <td>
							  <div class="btn-group">
								  <button type="button" onclick="addTr1()" class="btn btn-default" title="新增扩展属性"><i class="fa fa-file-o"></i> 新增扩展属性</button>

							  </div>
							  <table class="table table-bordered table-striped"  width="800px">
								  <thead>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue"></td>
									  <td>属性名称</td>
									  <td>操作</td>
								  </tr>
								  </thead>

								  <tbody id="cpTbody1">
								  <tr id="cpTr1">
									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
									  <td><input class="form-control" placeholder="属性名称" ></td>
									  <td><button type="button" onclick="delTr(this)" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								  </tr>
								  <tr>
									  <td><input type="checkbox" class="icheckbox_square-blue" ></td>
									  <td><input class="form-control" placeholder="属性名称" ></td>
									  <td><button type="button" onclick="delTr(this)" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>
								  </tr>
								  </tbody>
							  </table>

						  </td>
					  </tr>

				  </table>

			  </div>
			  <div class="modal-footer">
				  <button onclick="updateTypeTemplate()" class="btn btn-success" data-dismiss="modal" aria-hidden="true">保存</button>
				  <button class="btn btn-default" data-dismiss="modal" aria-hidden="true">关闭</button>
			  </div>
		  </div>
	  </div>
  </div>
    <script>

		//页面加载
		$(function () {
			getDate();
		})
		//搜索查询
		function searchName() {
			var name = $("#searchName").val();
			getDate(name)
		}
		//拼接数据
		function getDate(name) {
			$.ajax({
				url:"/templateController/templateList",
				type: "POST",
				dataType: "json",
				data: {name:name},
				async: false,
				success : function (data) {
					if (data.code == 10000){
						var tb = '';
						var typeList = data.data;
						for (let i = 0; i < typeList.length; i++) {
							tb += '<tr>';
							tb += '<td><input name="id" type="checkbox" value="' + typeList[i] .id+ '"></td>';
							tb += '<td>' + typeList[i].id + '</td>';
							tb += '<td>' + typeList[i].name + '</td>';
							tb += '<td>';
							var brandList = JSON.parse(typeList[i].brandIds);
							for (let j = 0; j < brandList.length; j++) {
								if (j == brandList.length-1){
									tb += brandList[j].text;
								}else {
									tb += brandList[j].text + ',';
								}
							}
							tb += '</td>';
							tb += '<td>';
							var specList = JSON.parse(typeList[i].specIds);
							for (let z = 0; z < specList.length; z++) {
								if (z == specList.length-1){
									tb += specList[z].text;
								}else {
									tb += specList[z].text +',';
								}
							}
							tb += '</td>';
							tb += '<td>';
							var customList = JSON.parse(typeList[i].customAttributeItems);
							for (let z = 0; z < customList.length; z++) {
								if (z == customList.length-1){
									tb += customList[z].text;
								}else {
									tb += customList[z].text + ',';
								}
							}
							tb += '</td>';
							tb += '<td class="text-center">';
							tb += '<button type="button" onclick="editTypeTemplate('+typeList[i] .id+')" class="btn bg-olive btn-xs" data-toggle="modal" data-target="#updateModal" >修改</button>';
							tb += '</td>';
							tb += '</tr>';

						}
						$("#typeTemplate").html(tb)
					}
				},error : function (data) {
					alert(data.message);
				}
			})
		}

		//下拉框
		$(function () {
			$("#brand").select2();
			$("#spec").select2();
			$("#brandUpdate").select2();
			$("#specUpdate").select2();
		})
		//添加规格增加一行
		function addTr() {
			var clone = $("#cpTr").clone();
			$("#cpTbody").append(clone)
			//将新增的数据清空
			$(clone).find(":input").val('');
		}
		//修改规格增加一行
		function addTr1() {
			var clone = $("#cpTr1").clone();
			$("#cpTbody1").append(clone)
			//将新增的数据清空
			$(clone).find(":input").val('');
		}
		//添加规格删除一行
		function delTr(nowTr) {
			$(nowTr).parent().parent().remove();
		}
		//添加方法
		function addTypeTemplate() {
			var name = $("#name").val(); //名字

			var brandList = $("#brand").select2('data');
			var brandArr = [];
			$(brandList).each(function (i,e) {
				var json = {id: e.id, text: e.text}
				brandArr.push(json)
			})
			var brandJson = JSON.stringify(brandArr);
			console.log(brandJson);

			var specList = $("#spec").select2('data');
			var specArr = [];
			$(specList).each(function (i,e) {
				var json = {id: e.id, text: e.text}
				specArr.push(json)
			})
			var specJson = JSON.stringify(specArr);
			console.log(specJson);

			let customAttributeItemsArr = [];
			var trList = $("#cpTbody").find("tr");
			$(trList).each(function (i,e) {
				var tdList = $(e).find("td");
				var text = $(tdList[1]).find("input").val();
				var json = {text:text};
				customAttributeItemsArr.push(json)
			})
			var customAttributeItems = JSON.stringify(customAttributeItemsArr);
			console.log(customAttributeItems)
			$.ajax({
				url:"/templateController/addTypeTemplate",
				type: "POST",
				dataType: "json",
				data:{name:name,specIds:specJson,brandIds:brandJson,customAttributeItems:customAttributeItems},
				async: false,
				success: function (data) {
					if (data.code == 10000){
						alert(data.message);
						window.location.reload();
					}
				},error: function (data) {
					alert(data.message);
				}

			})
		}

		//回显操作
		function editTypeTemplate(id) {
			$.ajax({
				url: "templateController/getTypeTemplateById",
				type: "post",
				dataType: "json",
				data:{id:id},
				async: false,
				success: function (data) {
					console.log(data)
					if (data.code == 10000){
						$("#nameUpdate").val(data.data.name)
						$("#id").val(data.data.id)
						var typeTemplate = data.data;
						var brandList = JSON.parse(typeTemplate.brandIds);
						var brandArr = [];
						$(brandList).each(function (i,e) {
							brandArr.push(e.id);
						});
						$("#brandUpdate").val(brandArr).trigger("change")

						var specList = JSON.parse(typeTemplate.specIds);
						var specArr = [];
						$(specList).each(function (i,e) {
							specArr.push(e.id);
						});
						$("#specUpdate").val(specArr).trigger("change")

						var customList = JSON.parse(typeTemplate.customAttributeItems);
						var table = '';
						for (let i = 0; i < customList.length; i++) {
							tr = '<tr id="cpTr1">\n' +
									'<td><input type="checkbox" class="icheckbox_square-blue" ></td>\n' +
									'<td><input class="form-control" value="' + customList[i].text + '" /></td>placeholder="属性名称" ></td>\n' +
									'<td><button type="button" onclick="delTr(this)" class="btn btn-default" title="删除"><i class="fa fa-trash-o"></i> 删除</button></td>\n' +
									'</tr>'
							table += tr;
						}
						$("#cpTbody1").html(table)

					}
				},error: function (data) {
					alert(data.message);
				}
			})
		}

		//修改操作
		function updateTypeTemplate() {
			//id
			var id = $("#id").val();
			var name = $("#nameUpdate").val();
			var brandList = $("#brandUpdate").select2('data');
			var brandArr = [];
			$(brandList).each(function (i,e) {
				var json = {id: e.id, text: e.text}
				brandArr.push(json)
			})
			var brandJson = JSON.stringify(brandArr);
			console.log(brandJson);

			var specList = $("#specUpdate").select2('data');
			var specArr = [];
			$(specList).each(function (i,e) {
				var json = {id: e.id, text: e.text}
				specArr.push(json)
			})
			var specJson = JSON.stringify(specArr);
			console.log(specJson);

			let customAttributeItemsArr = [];
			var trList = $("#cpTbody1").find("tr");
			$(trList).each(function (i,e) {
				var tdList = $(e).find("td");
				var text = $(tdList[1]).find("input").val();
				var json = {text:text};
				customAttributeItemsArr.push(json)
			})
			var customAttributeItems = JSON.stringify(customAttributeItemsArr);
			console.log(customAttributeItems)
			$.ajax({
				url:"/templateController/updateTypeTemplate",
				type: "POST",
				dataType: "json",
				data:{id:id,name:name,specIds:specJson,brandIds:brandJson,customAttributeItems:customAttributeItems},
				async: false,
				success: function (data) {
					if (data.code == 10000){
						alert(data.message);
						window.location.reload();
					}
				},error: function (data) {
					alert(data.message);
				}

			})
		}
		
		//批量删除
		function deleteCheck() {
			let ids = [];
			let idsArr = document.getElementsByName("id");
			for(let i=0; i<idsArr.length; i++){
				if(idsArr[i].checked){
					ids.push(idsArr[i].value);
				}
			}
			if (ids.length > 0){
				let flag = confirm("您确定要删除吗?");
				if (flag){
					$.get("templateController/deleteTypeTemplate","ids="+ids,function (data) {
						if (data.code == 10000){
							alert("删除成功");
							window.location.reload();
						}else {
							alert("删除失败");
						}
					})
				}else {
					alert("删除失败");
				}
			}else {
				alert("请您选择要删除的行");
			}

		}
	</script>
</body>

</html>